package controllers;

import java.util.List;

import models.UserConfig;
import play.Logger;
import play.mvc.Controller;
import util.NumberValidator;
import util.UnitConverter;
import ch.bfh.abch.rems.REMSMulticastController;
import ch.bfh.abch.rems.sender.tinkerforge.TFMulticastSender;
import ch.bfh.abcm.rems.konfiguration.UpdateUserConfigMessage;
import controllers.validation.SensorJobStarter;

public class UserConfigController extends Controller {

    public static void index() {
        List<models.UserConfig> userConfigList = models.UserConfig.findAll();
        render(userConfigList);
    }

    public static void setPropertyValue(String key, String value) {
        if (NumberValidator.isValidNumber(value)) {
            String TFValue = UnitConverter.convertNormValuetoTFValue(key, value);
            UserConfig userconfig = UserConfig.getProperty(key);
            userconfig.setPropertyValue(TFValue);
            userconfig.save();
            Logger.debug("Key: %s,  userValue: %s TFValue %s", key, value, TFValue);
            sendUserconfigUpdateToOtherNodes(key, TFValue);
        } else {
            flash.error("Es sind nur ganze Zahlen zwischen -999999 und 999999 gültig!");
        }
        index();
    }

    private static void sendUserconfigUpdateToOtherNodes(String key, String value) {
        UpdateUserConfigMessage updateUserConfigMessage = new UpdateUserConfigMessage(key, value);
        TFMulticastSender multicastSender = new REMSMulticastController().getMulticastSender();
        multicastSender.sendUpdateUserConfigMessage(updateUserConfigMessage);
    }

    public static void runValidationjob() {
        new SensorJobStarter().startValidationJob();
        index();
    }

    public static void insertDefaultUserConfig() {
        UserConfig.deleteAll();

        new UserConfig(UserConfig.KEY_SENSOR_MAX_TIME_DIFF_MS, "10000", "Sekunden seit letztem Wert").save();
        new UserConfig(UserConfig.KEY_NODEAVERAGE_MAX_TIME_DIFF_MS, "15000", "Sekunden  seit letztem Wert").save();

        new UserConfig(UserConfig.TEMPERATURE_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.MOISTURE_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.HUMIDITY_MAX_DIVERGENT, "400", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.AMBIENT_MAX_DIVERGENT, "250", "Sensorfehertoleranz").save();

        // Maximum age of sensor values (Aktualität)
        // Maximum and minimum of effective values (Schwellwerte)
        new UserConfig(UserConfig.TEMPERATURE_MAX_VALUE_USER, "3500", "Temperatur Obergrenze").save();
        new UserConfig(UserConfig.TEMPERATURE_MIN_VALUE_USER, "170", "Temperatur Untergrenze").save();

        new UserConfig(UserConfig.MOISTURE_MAX_VALUE_USER, "1500", "Feuchtigkeit Obergrenze").save();
        new UserConfig(UserConfig.MOISTURE_MIN_VALUE_USER, "0", "Feuchtigkeit Untergrenze").save();

        new UserConfig(UserConfig.AMBIENT_MAX_VALUE_USER, "2000", "Lux Obergrenze").save();
        new UserConfig(UserConfig.AMBIENT_MIN_VALUE_USER, "200", "Lux Untergrenze").save();

        new UserConfig(UserConfig.HUMIDITY_MAX_VALUE_USER, "535", "Luftfeuchtigkiet Obergrenze").save();
        new UserConfig(UserConfig.HUMIDITY_MIN_VALUE_USER, "100", "Luftfeuchtigkiet Untergrenze").save();
        index();
    }
}